package com.example;

import java.io.FileOutputStream;
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;

public class TestProxy {

    public static void main(String[] args) throws Exception {
        byte[] dump = $Proxy0Dump.dump();

//        FileOutputStream os = new FileOutputStream("$Proxy0.class");
//        os.write(dump, 0, dump.length);
//        os.close();

        // 创建一个新的类加载器，用于加载特定的字节码数据
        ClassLoader loader = new ClassLoader() {

            // 重写findClass方法，定义如何加载类
            @Override
            protected Class<?> findClass(String name) throws ClassNotFoundException {
                // 使用defineClass方法从字节码数据创建一个类对象
                return super.defineClass(name, dump, 0, dump.length);
            }
        };

        // 使用类加载器加载代理类
        Class<?> proxyClass = loader.loadClass("com.example.$Proxy0");

        Constructor<?> constructor = proxyClass.getConstructor(InvocationHandler.class);
        Foo proxy = (Foo) constructor.newInstance(new InvocationHandler() {
            @Override
            public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
                System.out.println("before...");
                System.out.println("调用目标");

                return null;
            }
        });

        proxy.foo();
    }
}
